package com.linciping.useges.plugin.util;

import com.intellij.psi.PsiFile;
import com.linciping.useges.plugin.bean.File2R;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


public class XmlResSaxHandler extends DefaultHandler {

    private PsiFile psiFile;

    public void createViewList(PsiFile psiFile, String string) throws ParserConfigurationException, SAXException, IOException {
        this.psiFile = psiFile;
        InputStream xmlStream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
        createViewList(psiFile, xmlStream);
    }

    public void createViewList(PsiFile psiFile, InputStream xmlStream) throws ParserConfigurationException, SAXException, IOException {
        this.psiFile = psiFile;
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse(xmlStream, this);
    }


    @Override
    public void startDocument() {
        stringConstantList = new ArrayList<>();
        attrConstantList = new ArrayList<>();
        boolConstantList = new ArrayList<>();
        colorConstantList = new ArrayList<>();
        dimeConstantList = new ArrayList<>();
        integerConstantList = new ArrayList<>();
        styleConstantList = new ArrayList<>();
        idsConstantList = new ArrayList<>();
        arrayConstantList = new ArrayList<>();
    }

    private static final String XML_STRING = "string";
    private static final String STRING_PREFIX = "R.string.";
    private static final String XML_ATTR = "declare-styleable";
    private static final String ATTR_PREFIX = "R.styleable.";
    private static final String XML_BOOL = "bool";
    private static final String BOOL_PREFIX = "R.bool.";
    private static final String XML_COLOR = "color";
    private static final String COLOR_PREFIX = "R.color.";
    private static final String XML_DIMEN = "dimen";
    private static final String DIMEN_PREFIX = "R.dime.";
    private static final String XML_INTEGER = "integer";
    private static final String INTEGER_PREFIX = "R.integer.";
    private static final String XML_STYLE = "style";
    private static final String STYLE_PREFIX = "R.style.";
    private static final String XML_ITEM = "item";
    private static final String XML_ID = "id";
    private static final String ID_PREFIX = "R.id.";
    private static final String XML_ARRAY = "array";
    private static final String ARRAY_PREFIX = "R.array";

    private List<File2R> stringConstantList;
    private List<File2R> attrConstantList;
    private List<File2R> boolConstantList;
    private List<File2R> colorConstantList;
    private List<File2R> dimeConstantList;
    private List<File2R> integerConstantList;
    private List<File2R> styleConstantList;
    private List<File2R> idsConstantList;
    private List<File2R> arrayConstantList;

    public List<File2R> getStringConstantList() {
        return stringConstantList;
    }

    public List<File2R> getAttrConstantList() {
        return attrConstantList;
    }

    public List<File2R> getBoolConstantList() {
        return boolConstantList;
    }

    public List<File2R> getColorConstantList() {
        return colorConstantList;
    }

    public List<File2R> getDimeConstantList() {
        return dimeConstantList;
    }

    public List<File2R> getIntegerConstantList() {
        return integerConstantList;
    }

    public List<File2R> getStyleConstantList() {
        return styleConstantList;
    }

    public List<File2R> getIdsConstantList() {
        return idsConstantList;
    }

    public List<File2R> getArrayConstantList() {
        return arrayConstantList;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        switch (qName) {
            case XML_STRING:
                String name = attributes.getValue("name");
                String constantName = STRING_PREFIX + name;
                File2R file2R = new File2R(psiFile, name, constantName);
                stringConstantList.add(file2R);
                break;
            case XML_ATTR:
                name = attributes.getValue("name");
                constantName = ATTR_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                attrConstantList.add(file2R);
                break;
            case XML_BOOL:
                name = attributes.getValue("name");
                constantName = BOOL_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                boolConstantList.add(file2R);
                break;
            case XML_COLOR:
                name = attributes.getValue("name");
                constantName = COLOR_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                colorConstantList.add(file2R);
                break;
            case XML_DIMEN:
                name = attributes.getValue("name");
                constantName = DIMEN_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                dimeConstantList.add(file2R);
                break;
            case XML_INTEGER:
                name = attributes.getValue("name");
                constantName = INTEGER_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                integerConstantList.add(file2R);
                break;
            case XML_STYLE:
                name = attributes.getValue("name");
                constantName = STYLE_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                styleConstantList.add(file2R);
                break;
            case XML_ARRAY:
                name = attributes.getValue("name");
                constantName = ARRAY_PREFIX + name;
                file2R = new File2R(psiFile, name, constantName);
                arrayConstantList.add(file2R);
                break;
            case XML_ITEM:
                String type = attributes.getValue("type");
                name = attributes.getValue("name");
                if (type != null && type.equals(XML_ID)) {
                    constantName = ID_PREFIX + name;
                    file2R = new File2R(psiFile, name, constantName);
                    idsConstantList.add(file2R);
                }
                break;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    }

}
